unit Main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, XPMan, StdCtrls, Buttons, ExtCtrls, SDK, pngimage;

type
  TfrmMain = class(TForm)
    Image1: TImage;
    Label2: TLabel;
    edtFile: TEdit;
    btnBrowse: TBitBtn;
    XPManifest1: TXPManifest;
    GroupBox1: TGroupBox;
    LogBox: TListBox;
    chkRealignFile: TCheckBox;
    btnExit: TBitBtn;
    btnAbout: TBitBtn;
    btnUnpack: TBitBtn;
    OpenDialog1: TOpenDialog;
    Label1: TLabel;
    cbbPacker: TComboBox;
    Label3: TLabel;
    procedure btnBrowseClick(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure btnAboutClick(Sender: TObject);
    procedure btnUnpackClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure LoadUnpack;
  public
    { Public declarations }

  end;

var
  frmMain: TfrmMain;


implementation

uses uUnpack;

{$R *.dfm}


procedure TfrmMain.btnBrowseClick(Sender: TObject);
begin
  if OpenDialog1.Execute then
  begin
    edtFile.Text := OpenDialog1.FileName;
  end;
end;

procedure TfrmMain.btnExitClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmMain.btnUnpackClick(Sender: TObject);
var
  aUnpack: TUnpack;
begin
  edtFile.Text := Trim(edtFile.Text);
  if (edtFile.Text = '') or (not FileExists(edtFile.Text)) or
    (cbbPacker.ItemIndex = -1) then
    Exit;

  aUnpack := TUnpackClass(UnpackLists.Items[cbbPacker.ItemIndex]).Create();
  try
    try
      aUnpack.Option.IsRealignPE := chkRealignFile.Checked;
      aUnpack.UnPackFile := edtFile.Name;
      aUnpack.Unpack;
    except
      on e: Exception do
      begin
        Application.MessageBox(PChar(e.Message), 'Fail', MB_ICONERROR);
      end;
    end;

  finally
    aUnpack.Free;
  end;
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  LoadUnpack;
end;

procedure TfrmMain.LoadUnpack;
var
  i: integer;
begin
  cbbPacker.Clear;

  for i := 0 to UnpackLists.Count - 1 do
  begin
    cbbPacker.Items.Add(TUnpackClass(UnpackLists.Items[i]).GetUnpackName);
  end;

  if cbbPacker.Items.Count > 0 then
    cbbPacker.ItemIndex := 0;

end;

procedure TfrmMain.btnAboutClick(Sender: TObject);
begin
  messagedlg('Tinyunpacker' + #13#10#13#10 +
    'Visit Website at http://code.google.com/p/tinyunpacker/' + #13#10#13#10 +
    '  Minimum engine version needed:' + #13#10 +
    '- TitanEngine 2.0 by RevLabs' + #13#10#13#10 +
    'Unpacker coded by sandy', mtInformation, [mbOk], 0);
end;

end.
